package cn.bby.tools.actions.str.convert;

import cn.bby.tools.service.StateManagementService;
import cn.bby.tools.utils.AnActionEventUtils;
import cn.bby.tools.utils.split.LinkWordSplitUtilImpl;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.MessageDialogBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @description: IdentifyWordAddCustomWord
 * @author: ben
 * @date: 2025/9/28 10:36
 * @version: 1.0
 */
public class IdentifyWordAddCustomWord extends AnAction {
    private final IdentifyWord.IdentifyWordState wordState = StateManagementService.getInstanceState().identifyWordState;
    private final LinkWordSplitUtilImpl wordSplitUtil = LinkWordSplitUtilImpl.getInstance();

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String selectedText = AnActionEventUtils.getSelectedText(e);
        if (StrUtil.isEmpty(selectedText)) {
            return;
        }
        if (selectedText.length() > 20) {
            MessageDialogBuilder.yesNo("警告", "选中的字符过长（不能大于20），请重新输入！")
                    .ask(e.getProject());
        }
        wordState.customWords.add(selectedText);
        wordSplitUtil.addWords(List.of(selectedText));
    }
}
